package com.asjy.service.impl;

import com.asjy.dao.OrderDao;
import com.asjy.dao.UserDao;
import com.asjy.po.*;
import com.asjy.service.OrderService;
import org.omg.PortableInterceptor.Interceptor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.enterprise.inject.Default;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: lilinlin
 * @Date: 2021/4/16
 * @Description: com.asjy.service.impl
 * @version: 1.0
 */
@Service

public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Override
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    public Integer saveOrderAndItem(Order order, Cart cart)  {
       try {
           //1.持久化订单信息
           orderDao.saveOrder(order);
           //2.持久化订单项信息
           Collection<CartItem> cartItemList = cart.getCartItemMap();
           OrderItem orderItem  = new OrderItem();
           for (CartItem cartItem : cartItemList) {
               //将每个购物项保存在订单项中
               orderItem.setCount(cartItem.getCount());
               orderItem.setoId(order.getoId());
               orderItem.setpId(cartItem.getProduct().getpId());
               orderItem.setSubTotal(cartItem.getSubTotal());
               orderDao.saveOrderItem(orderItem);
           }
       }catch (Exception ex){
           //处理业务逻辑
           ex.printStackTrace();
           throw new RuntimeException("被0除了");
       }
      return order.getoId();

    }

    @Override
    public OrderExt getOrderById(Integer oId) {
        return orderDao.getOrderInfo(oId);
    }

    @Override
    public OrderPageBean getOrdersByUid(OrderPageBean orderPageBean) {//pageNow uId
        //1.获取用户的订单数量 rowCount
        Integer orderCount = orderDao.getOrderCountByUid(orderPageBean.getuId());
        orderPageBean.setRowCount(orderCount);
        //2.获取订单信息 start pageSize uId
        orderPageBean.setPageSize(3);
        List<OrderExt> orderExtList = orderDao.getOrderByUid(orderPageBean);
        orderPageBean.setList(orderExtList);
        //pageNow uId rowCount pageCount  list pageSize
        return orderPageBean;
    }

    @Override
    public Map<Long, Integer> getOrderCountByState(Integer uId) {
        Map<Long,Integer> statMap = new HashMap<>();
        List<Map<String, Integer>> orderCountByState = orderDao.getOrderCountByState(uId);
        for (Map<String, Integer> map : orderCountByState) {
            statMap.put(map.get("state").longValue(),map.get("count(*)"));
        }
        return statMap;
    }

    @Override
    public void changeState(Order order) {
        orderDao.changeState(order);
    }


}